package com.beiding.listener;

import com.beiding.dao.OrderDao;
import com.beiding.pojo.order.Order;
import com.beiding.service.RoomStorageService;
import com.beiding.service.impl.MessageSender;
import com.beiding.timeout.bean.TimeShaft;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class Refund extends TimeoutListener {

    private RoomStorageService roomStorageService;

    @Autowired
    public void setRoomStorageService(RoomStorageService roomStorageService) {
        this.roomStorageService = roomStorageService;
    }



    private MessageSender sender;

    @Autowired
    public void setSender(MessageSender sender) {
        this.sender = sender;
    }

    protected Refund(OrderDao orderDao, TimeShaft<String> timeShaft) {
        super(orderDao, timeShaft, "refund");
    }

    @Override
    public void run(String id) {
        Order order = orderDao.getInsensitive(id);

        if (judge(order, Order.REFUND)) {
            orderDao.updateField(order.getId(), "state", Order.OVERDUE);
            roomStorageService.release(order.getRoomId());
            refund(order);

            order.setState(Order.OVERDUE);

            sender.sendToTenant(order.getHolderId(),update(order).msg("你有一笔订单自动退款"));

            sender.sendToLandlord(order.getLandlord(), update(order).msg("房客发起退款,但您超时未处理,已经自动退款"));

        }
    }

    private void refund(Order order) {
        System.out.println("超时未处理退款请求自动退款");
    }
}

